package com.study.service.db3.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.study.entity.db3.AccountRole;
import com.study.entity.db3.Role;
import com.study.entity.db3.RoleResource;
import com.study.entity.db3.qo.RoleQO;
import com.study.entity.db3.vo.RoleVO;
import com.study.enumeration.DeletedEnum;
import com.study.enumeration.ResponseEnum;
import com.study.exception.DefinitionException;
import com.study.mapper.db3.AccountRoleMapper;
import com.study.mapper.db3.RoleMapper;
import com.study.mapper.db3.RoleResourceMapper;
import com.study.service.db3.IRoleService;
import com.study.util.BaseUtils;

/**
 * 角色表 服务实现类
 * 
 * @author chenk
 *
 */
@Service
public class RoleServiceImpl implements IRoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RoleResourceMapper roleResourceMapper;

	@Autowired
	private AccountRoleMapper accountRoleMapper;

	@Override
	public List<RoleVO> queryRolesForSelect(Long accountId) {
		RoleQO role = new RoleQO();
		role.setDeleted(Integer.valueOf(DeletedEnum.否.getCode()));
		List<RoleVO> roles = roleMapper.queryRoles(role);
		if (accountId != null) {
			List<AccountRole> accountRoles = accountRoleMapper.queryByAccountId(accountId);
			List<Long> collect = accountRoles.stream().map(AccountRole::getRoleId).collect(Collectors.toList());
			for (RoleVO vo : roles) {
				if (collect.contains(vo.getValue())) {
					vo.setSelected(true);
				}
			}
		}
		return roles;
	}

	@Override
	public Page<Role> queryPage(Map<String, Object> requestParams) {
		RoleQO role = BaseUtils.mapToEntity(requestParams, RoleQO.class);
		role.setDeleted(Integer.valueOf(DeletedEnum.否.getCode()));
		return roleMapper.queryPage(role);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveRole(Role role) {
		roleMapper.saveRole(role);
		Long roleId = role.getRoleId();

		// 选中的角色
		List<Long> resourceIds = role.getResourceIds();

		if (resourceIds != null && resourceIds.size() > 0) {
			for (Long resourceId : resourceIds) {
				RoleResource roleResource = new RoleResource();
				roleResource.setRoleId(roleId);
				roleResource.setResourceId(resourceId);
				roleResourceMapper.saveRoleResource(roleResource);
			}
		}

	}

	@Override
	public void deleteById(Long id) {

		int count = accountRoleMapper.countAccountRoleByRoleId(id);
		if (count > 0) {
			throw new DefinitionException(ResponseEnum.EXIST_USER_BY_ROLE);
		}
		
		Map<String, Object> paramMap = new HashMap<>();
		// 逻辑删除标识(0、否 1、是)
		paramMap.put("deleted", "1");
		paramMap.put("roleId", id);
		roleMapper.deleteById(paramMap);
	}

	@Override
	public Role getById(Long id) {
		RoleQO role = new RoleQO();
		role.setRoleId(id);
		return roleMapper.getById(role);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateRole(Role role) {
		roleMapper.updateRole(role);

		Long roleId = role.getRoleId();

		roleResourceMapper.deleteByRoleId(roleId);

		List<Long> resourceIds = role.getResourceIds();

		if (resourceIds != null && resourceIds.size() > 0) {
			for (Long resourceId : resourceIds) {
				RoleResource roleResource = new RoleResource();
				roleResource.setRoleId(roleId);
				roleResource.setResourceId(resourceId);
				roleResourceMapper.saveRoleResource(roleResource);
			}
		}
	}

}
